/**
 * 为food_data.json中的食物添加英文名
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 中文到英文的映射
const nameMapping = {
  '膳力达水解乳清蛋白复合营养粉': 'Shanlida Hydrolyzed Whey Protein Complex Nutrition Powder',
  '有机核桃芝麻黑豆粉': 'Organic Walnut Sesame Black Bean Powder',
  '猴头菇石斛山药粉': 'Hericium Mushroom Dendrobium Yam Powder',
  '黄豆腐': 'Yellow Tofu',
  '黑豆腐': 'Black Tofu',
  '鸡蛋': 'Egg',
  '鹰嘴豆': 'Chickpea',
  '雅培安素倍佳': 'Abbott Ensure Beijia',
  '牛肉': 'Beef',
  '银鳕鱼': 'Silver Cod Fish',
  '海参': 'Sea Cucumber',
  '三文鱼': 'Salmon',
  '黄鱼': 'Yellow Croaker',
  '鸡肉': 'Chicken',
  '虾': 'Shrimp',
  '花胶': 'Fish Maw',
  '鲳鱼': 'Pomfret',
  '小米': 'Millet',
  '粳米': 'Japonica Rice',
  '藜麦米': 'Quinoa Rice',
  '燕麦片': 'Oatmeal',
  '板栗': 'Chestnut',
  '红薯': 'Sweet Potato',
  '西红柿': 'Tomato',
  '紫甘蓝': 'Purple Cabbage',
  '绿甘蓝': 'Green Cabbage',
  '木耳': 'Wood Ear Mushroom',
  '南瓜': 'Pumpkin',
  '杏鲍菇': 'King Oyster Mushroom',
  '秋葵': 'Okra',
  '豌豆苗': 'Pea Shoots',
  '银耳': 'White Fungus',
  '胡萝卜': 'Carrot',
  '白萝卜': 'White Radish',
  '羽衣甘蓝': 'Kale',
  '茄子': 'Eggplant',
  '山药': 'Chinese Yam',
  '娃娃菜': 'Baby Cabbage',
  '莲子': 'Lotus Seed',
  '土豆': 'Potato',
  '黄辣椒': 'Yellow Pepper',
  '红辣椒': 'Red Pepper',
  '西兰花': 'Broccoli',
  '韭菜': 'Chinese Chive',
  '菠菜': 'Spinach',
  '黄瓜': 'Cucumber',
  '百合': 'Lily Bulb',
  '芹菜': 'Celery',
  '绿豆芽': 'Mung Bean Sprouts',
  '青菜白菜': 'Bok Choy',
  '红洋葱': 'Red Onion',
  '姜': 'Ginger',
  '芦笋': 'Asparagus',
  '芋头': 'Taro',
  '蕃茄': 'Tomato',
  '生菜': 'Lettuce',
  '芹菜汁': 'Celery Juice',
  '梨': 'Pear',
  '苹果': 'Apple',
  '牛油果': 'Avocado',
  '西梅': 'Prune',
  '猕猴桃': 'Kiwi',
  '蓝莓': 'Blueberry',
  '橄榄油': 'Olive Oil',
  '芝麻粉': 'Sesame Powder',
  '核桃': 'Walnut',
  '南瓜子': 'Pumpkin Seeds',
  '松子': 'Pine Nuts',
  '巴西莓粉': 'Acai Berry Powder',
  '亚麻籽': 'Flaxseed',
  '秋葵[黄秋葵，羊角豆]': 'Okra',
  '对虾': 'Prawn',
  '藕[莲藕]': 'Lotus Root',
  '明月梨': 'Mingyue Pear',
  '石榴（玛瑙）': 'Pomegranate',
  '意大利玛馥嘉拉菲尔特级初榨橄榄油': 'Italian Maffucci Raphael Extra Virgin Olive Oil',
  '亚麻籽油': 'Flaxseed Oil'
};

// 读取food_data.json
const foodDataPath = path.join(__dirname, '../database/food_data.json');
const foodData = JSON.parse(fs.readFileSync(foodDataPath, 'utf8'));

console.log(`读取到 ${foodData.length} 种食物`);

// 更新英文名
let updatedCount = 0;
let missingCount = 0;
const missingNames = [];

foodData.forEach(food => {
  if (nameMapping[food.name_zh]) {
    food.name_en = nameMapping[food.name_zh];
    updatedCount++;
  } else {
    missingNames.push(food.name_zh);
    missingCount++;
  }
});

console.log(`✅ 已更新 ${updatedCount} 个食物的英文名`);

if (missingCount > 0) {
  console.log(`⚠️  还有 ${missingCount} 个食物缺少英文名:`);
  missingNames.forEach((name, index) => {
    console.log(`   ${index + 1}. ${name}`);
  });
}

// 为ID=1的食物添加other_nutrients
console.log('\n📝 为膳力达水解乳清蛋白复合营养粉添加other_nutrients...');
const food1 = foodData.find(f => f.id === 1);
if (food1) {
  food1.other_nutrients = JSON.stringify([
    {
      nutrient_id: 1001,
      name_zh: '牛磺酸',
      name_en: 'Taurine',
      amount: 125,
      unit: 'mg',
      rich: true
    },
    {
      nutrient_id: 1002,
      name_zh: '泛酸（维生素B5）',
      name_en: 'Pantothenic Acid (Vitamin B5)',
      amount: 6,
      unit: 'mg',
      rich: true
    }
  ]);
  console.log('✅ 已添加牛磺酸: 125mg, 泛酸（维生素B5）: 6mg');
} else {
  console.log('⚠️  未找到ID=1的食物');
}

// 写回文件
fs.writeFileSync(foodDataPath, JSON.stringify(foodData, null, 4), 'utf8');
console.log('\n✅ 已保存到 database/food_data.json');

